<?php

declare(strict_types=1);
/*
 * @Description: 钉钉service类
 * @Author: QianLong
 * @Date: 2019-10-13 12:39:05
 * @LastEditors    : QianLong
 * @LastEditTime   : 2023-03-10 11:12:18
 */

namespace App\Service\Push;

use App\Common\RedisCache;
use Yurun\Util\HttpRequest;

class DdWork
{
    protected $serverInfo = [];
    protected $http = null;
    protected $redisCache = null;
    public function __construct($serverInfo)
    {
        $this->serverInfo = $serverInfo;
        $this->http = new HttpRequest;
        $this->redisCache = new RedisCache();
    }

    /**
     * 群机器人
     */
    public function pushBotText(array $params)
    {
        $postDDork['msgtype'] = 'text';
        $postDDork['text']['content'] = $params['msg_content'];
        if (!isset($params['atMobiles']) && !empty($params['atMobiles'])) {
            $allAtUsers = explode(',', $params['atMobiles']);
            foreach ($allAtUsers as $key => $vo) {
                if (!empty($vo)) {
                    if ($vo == 'all') {
                        $postDDork['text']['at']['isAtAll'] = true;
                    } else {
                        $postDDork['text']['at']['atMobiles'][] = $vo;
                    }
                }
            }
        }
        list($microsecond, $time) = explode(' ', microtime()); //' '中间是一个空格
        $timeStamp = sprintf('%.0f', (floatval($microsecond) + floatval($time)) * 1000);
        $sign = urlencode(base64_encode(hash_hmac('sha256', $timeStamp . "\n" . $this->serverInfo['platform_alert_dingding_secret'], $this->serverInfo['platform_alert_dingding_secret'], true)));
        $response = $this->http->post($this->serverInfo['platform_alert_dingding_webhook_url'] . '&timestamp=' . $timeStamp . '&sign=' . $sign, $postDDork, 'json');
        return json_decode($response->body(), true);
    }
    public function pushBotTuwen(array $params)
    {
        $postDDork['msgtype'] = 'link';
        $postDDork['link']['text'] = $params['content'];
        $postDDork['link']['title'] = $params['title'];
        $msgContent = json_decode($params['msg_content'], true);
        $postDDork['link']['messageUrl'] = isset($msgContent['url']) ? $msgContent['url'] : '';
        $postDDork['link']['picUrl'] = isset($msgContent['pic']) ? $msgContent['pic'] : '';
        $timeStamp = time() * 1000;
        $sign = urlencode(base64_encode(hash_hmac('sha256', $timeStamp . "\n" . $this->serverInfo['platform_alert_dingding_secret'], $this->serverInfo['platform_alert_dingding_secret'], true)));
        $response = $this->http->post($this->serverInfo['platform_alert_dingding_webhook_url'] . '&timestamp=' . $timeStamp . '&sign=' . $sign, $postDDork, 'json');
        return json_decode($response->body(), true);
    }
    public function pushBotMD(array $params)
    {
        $postDDork['msgtype'] = 'markdown';
        $postDDork['markdown']['title'] = $params['title'];
        $postDDork['markdown']['text'] = $params['msg_content'];
        if (!isset($params['atMobiles']) && !empty($params['atMobiles'])) {
            $allAtUsers = explode(',', $params['atMobiles']);
            foreach ($allAtUsers as $key => $vo) {
                if (!empty($vo)) {
                    if ($vo == 'all') {
                        $postDDork['text']['at']['isAtAll'] = true;
                    } else {
                        $postDDork['text']['at']['atMobiles'][] = $vo;
                    }
                }
            }
        }
        $timeStamp = time() * 1000;
        $sign = urlencode(base64_encode(hash_hmac('sha256', $timeStamp . "\n" . $this->serverInfo['platform_alert_dingding_secret'], $this->serverInfo['platform_alert_dingding_secret'], true)));
        $response = $this->http->post($this->serverInfo['platform_alert_dingding_webhook_url'] . '&timestamp=' . $timeStamp . '&sign=' . $sign, $postDDork, 'json');
        return json_decode($response->body(), true);
    }
}
